package com.quanyan.place.biz.redis.impl;

import com.quanyan.common.exception.BizException;
import com.quanyan.common.utils.ObjectUtil;
import com.quanyan.place.biz.redis.ConfigCacheDao;
import com.quanyan.place.cache.PlaceRedisTemplate;
import com.quanyan.place.constants.PlaceRedisConstans;
import com.quanyan.place.entity.TbCity;
import com.quanyan.place.entity.TbCityRegion;
import com.quanyan.place.entity.TbCommonParameter;
import com.quanyan.place.entity.TbDistrict;
import com.quanyan.place.entity.TbProvince;
import com.quanyan.place.entity.apiresp.RespCategory;
import com.quanyan.place.entity.apiresp.RespCityInfo;
import com.quanyan.place.entity.base.apiresp.RespDistrict;
import com.quanyan.redisCluster.utils.SerializeUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by dongsheng.lin on 2017/4/28.
 */
@Service
public class ConfigCacheDaoImpl implements ConfigCacheDao {

    private final static Logger logger = LoggerFactory.getLogger(ConfigCacheDaoImpl.class);

    /**
     * 过期时间
     */
    private final static int EXPIRE_TIME_MINUTE = 60 * 1;

    private final static int EXPIRE_TIME_FIVE_MINUTE = 60 * 5;

    private final static int EXPIRE_TIME_HOUR = 60 * 60;

    private final static int EXPIRE_TIME_DAY = 60 * 60 * 24;

    @Autowired
    private PlaceRedisTemplate redisTemplate;

    @Override
    public List<TbCommonParameter> getCommonParameterFromCache() {
        return (List<TbCommonParameter>)redisTemplate.get(PlaceRedisConstans.getAllCommonParameterKey());
    }

    @Override
    public void setCommonParameterToCache(List<TbCommonParameter> tbCommonParameters) {
        redisTemplate.setWithExpireTime(PlaceRedisConstans.getAllCommonParameterKey(), tbCommonParameters, EXPIRE_TIME_DAY);
    }

    @Override
    public List<RespCategory> getSysCategroyInfoFromCache() {
        return (List<RespCategory>)redisTemplate.get(PlaceRedisConstans.getSysCategroyInfoKey());
    }

    @Override
    public void setSysCategroyInfoToCache(List<RespCategory> respCategories) {
        redisTemplate.setWithExpireTime(PlaceRedisConstans.getSysCategroyInfoKey(),respCategories,EXPIRE_TIME_DAY);
    }

    @Override
    public List<TbProvince> getProvincesFromCache() {
        List<TbProvince> tbProvinces = new ArrayList<>();
        List<byte[]> provincesList = redisTemplate.lrange(PlaceRedisConstans.getProvincesKey(),0,-1);
        if(CollectionUtils.isNotEmpty(provincesList)){
            for(byte[] entry : provincesList){
                tbProvinces.add((TbProvince)SerializeUtil.unserialize(entry));
            }
        }
        return tbProvinces;
    }

    @Override
    public void setProvincesToCache(List<TbProvince> tbProvinces) {
        if(CollectionUtils.isEmpty(tbProvinces)){
            return;
        }
        for(TbProvince tbProvince : tbProvinces){
            redisTemplate.rpush(PlaceRedisConstans.getProvincesKey(), tbProvince);
        }
    }


    @Override
    public void setCitiesToCache(List<TbCity> tbCityList) {
        if(CollectionUtils.isNotEmpty(tbCityList)){
            for(TbCity tbCity : tbCityList){
                redisTemplate.rpush(PlaceRedisConstans.getCitiesKey(),tbCity);
            }
        }
    }

    @Override
    public List<TbCity> getCitiesFromCache() {
        List<TbCity> tbCities = new ArrayList<>();
        List<byte []> cityList = redisTemplate.lrange(PlaceRedisConstans.getCitiesKey(),0,-1);
        for(byte [] entry : cityList){
            tbCities.add((TbCity)SerializeUtil.unserialize(entry));
        }
        return tbCities;
    }


    @Override
    public void setDistrictsToCache(List<TbDistrict> tbDistricts) {
        if(CollectionUtils.isNotEmpty(tbDistricts)){
            for(TbDistrict tbDistrict : tbDistricts){
                redisTemplate.rpush(PlaceRedisConstans.getDistrictsKey(),tbDistrict);
            }
        }
    }

    @Override
    public List<TbDistrict> getDistrictsFromCache() {
        List<TbDistrict> tbDistricts = new ArrayList<>();
        List<byte []> districtList =  redisTemplate.lrange(PlaceRedisConstans.getDistrictsKey(), 0, -1);
        for(byte [] entry : districtList){
            tbDistricts.add((TbDistrict)SerializeUtil.unserialize(entry));
        }
        return tbDistricts;
    }

    @Override
    public List<RespDistrict> getDistrictsByCityIdFromCache(int cityId) {
        List<RespDistrict> respDistricts = new ArrayList<>();
        RespCityInfo respCityInfo = (RespCityInfo)redisTemplate.get(PlaceRedisConstans.getDistrictsByCityIdKey(cityId));
        respDistricts.addAll(respCityInfo.getRespDistrict());
        return respDistricts;
    }

    @Override
    public void setDistrictsByCityIdToCache(RespCityInfo respCityInfo) {
        if(ObjectUtil.isNotObjectEmpty(respCityInfo)){
            redisTemplate.set(PlaceRedisConstans.getDistrictsByCityIdKey(respCityInfo.getRespCity().getId()),respCityInfo);
        }
    }

    @Override
    public void setDictsByTagGroupToCache(String tagGroup,HashMap<String,String> dictMap) {
        if(StringUtils.isNotEmpty(tagGroup) && !dictMap.isEmpty()){
            redisTemplate.set(PlaceRedisConstans.getDictsByTagGroupKey(tagGroup),dictMap);
        }
    }

    @Override
    public HashMap<String, String> getDictsByTagGroupToCache(String tagGroup) {
        if(StringUtils.isEmpty(tagGroup)){
            logger.error("tagGroup不能为空！");
            throw new BizException("tagGroup不能为空！");
        }
        return (HashMap<String,String>)redisTemplate.get(PlaceRedisConstans.getDictsByTagGroupKey(tagGroup));
    }

    @Override
    public List<TbCityRegion> getRegionByCityIdFromCache() {
        return (List<TbCityRegion>)redisTemplate.get(PlaceRedisConstans.getCityRegionKey());
    }

    @Override
    public void setCityRegionsToCache(List<TbCityRegion> cityRegionList) {
        if(CollectionUtils.isNotEmpty(cityRegionList)){
            redisTemplate.set(PlaceRedisConstans.getCityRegionKey(),cityRegionList);
        }
    }

}
